package DataStructure.LinkedList;

/**
 * @Author：DijaH
 * @Date：2023/2/5 12:50 下午
 * @Version：1.0.0
 * @Description：
 * 反转链表区间 m...n
 */
public class reverseMN {
    public ListNode reverseMToN(ListNode head,int m,int n){
        //base case
        if(m == 1){
            return reverseN(head,n);
        }
        head.next = reverseMToN(head.next,m - 1, n -1);
        return head;
    }

    public ListNode reverseN(ListNode head,int n){
        //前N个反转
        //base case
        ListNode cur = null;
        if(n == 1){
            cur = head.next;
            return head;
        }
        ListNode newNode = reverseN(head.next,n - 1);
        head.next.next = head;
        head.next = cur;
        return newNode;
    }
}
